package com.hpx.javaeye.business;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.xml.jaxp.DOMInternalRepresentation;

import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public abstract class RenJianBusiness implements Business {
	protected HttpConnection c = null;

	public void processRequest(String target, String paras)
			throws SAXException, IOException {
		InputStream is = null;
		OutputStream os = null;
		int rc;
		c = (HttpConnection) Connector.open(target);
		System.out.println("connection is open......hpx debug.");
		// Set the request method and headers
		c.setRequestMethod(HttpConnection.GET);
		c.setRequestProperty("If-Modified-Since", "29 Oct 1999 19:43:31 GMT");
		c.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		c.setRequestProperty("Accept-Encoding", "gzip");
		c.setRequestProperty("User-Agent", "MyUserAgentHere");
		c.setRequestProperty("Content-Language", "en-US");
		c.setRequestProperty("Cache-Control",
				"no-store, no-cache, must-revalidate");

		pocessParameter(paras);

		// Getting the output stream may flush the headers
		os = c.openOutputStream();
		System.out.println("output is open");
		// Getting the response code will open the connection,
		// send the request, and read the HTTP response headers.
		// The headers are stored until requested.
		rc = c.getResponseCode();

		System.out.println("i got the response code rc is :" + rc);

		if (rc != HttpConnection.HTTP_OK) {
			throw new IOException("HTTP response code: " + rc);
		}

		is = c.openInputStream();
		System.out.println("input is open .");
		// Get the ContentType
		String type = c.getType();
		processType(type);

		// Get the length and process the data
		int len = (int) c.getLength();

		System.out.println("len is : " + len);

		if (len > 0) {
			int actual = 0;
			int bytesread = 0;
			byte[] data = new byte[len];
			while ((bytesread != len) && (actual != -1)) {
				actual = is.read(data, bytesread, len - bytesread);
				bytesread += actual;
			}
			process(data);
		} else {
			int ch;
			while ((ch = is.read()) != -1) {
				process((byte) ch);
			}
		}
	}

	protected abstract void process(byte ch);

	protected abstract void processType(String type);

	protected abstract void process(byte[] ch);

	private void pocessParameter(String paras) throws SAXException, IOException {
		DOMInternalRepresentation dom = new DOMInternalRepresentation();
		DocumentFragment df = dom.parseFragment(paras);
		NodeList list = df.getChildNodes();
		if (0 != list.getLength())
			list = list.item(0).getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			Node n = list.item(i);

			String noteName = n.getNodeName();
			String noteValue = n.getFirstChild().getNodeValue();
			c.setRequestProperty(noteName, noteValue);
		}
	}

}
